package cst.a931xmc.yuejian20.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a931xmc.yuejian20.Activity.MatchActivity;
import cst.a931xmc.yuejian20.Bean.MatchBean;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.R;

public class MatchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private LayoutInflater inflater;
    private List<MatchBean> list;
    private Context context;

    public MatchAdapter(Context context){
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setList(List<MatchBean> l){
        list = l;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = inflater.from(parent.getContext()).inflate(R.layout.item_hmatch, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MatchBean bean = list.get(position);
        if (bean == null)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.match_name.setText(bean.getMatch_name());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getLayoutPosition();
                Intent intent;
                intent = new Intent(context, MatchActivity.class);
                intent.putExtra("match_id", list.get(position).getMatch_id());
                context.startActivity(intent);
            }
        });

        ApiModel apiModel = new ApiModel();
        Picasso.with(context)
                .load(apiModel.getBaseUrl() + "static/images/" + bean.getMatch_pic())
                .placeholder(R.drawable.nopic)
                .into(viewHolder.match_pic);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (list == null) return 0;
        else return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView match_pic;
        TextView match_name;

        public ViewHolder(View itemView) {
            super(itemView);
            match_pic = itemView.findViewById(R.id.match_pic);
            match_name = itemView.findViewById(R.id.match_name);
        }
    }

}
